<?php

namespace App\Admin\Controllers;

use App\Models\City;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CityController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('商圈管理');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑商圈');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建商圈');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(City::class, function (Grid $grid) {
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('zonename', '市区县');
                $filter->like('province', '省份');
                $filter->like('city', '城市');
                $filter->like('street_address', '街道');
            });

            $grid->id('ID')->sortable();
            $grid->level('市区县')->display(function ($level){
                switch ($level){
                    case '0':
                        return '省';
                        break;
                    case '1':
                        return '市';
                        break;
                    case '2':
                        return '区';
                        break;
                    default:
                        return '';
                        break;
                }
            });
            $grid->column('zonename', '区县');
            $grid->column('code', '编号');
            $grid->column('parentid', '父级id');
            $grid->column('province', '省份');
            $grid->column('city', '城市');
            $grid->column('street_address', '街道');
            $grid->actions(function ($actions) {
                $city_id = $actions->getKey();
                $actions->prepend('<a href="/admin/city/add?city_id='.$city_id.'"><i class="fa fa-adn">添加商圈</i></a>');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(City::class, function (Form $form) {

            $form->saving(function (Form $form) {
                try {
                    City::insert([
                        'level' => $form->level,
                        'zonename' => $form->zonename,
                        'code' => $form->code,
                        'parentid' => $form->parentid,
                        'province' => $form->province,
                        'city' => $form->city,
                        'street_address' => $form->street_address
                    ]);
                    echo "<script> alert('添加商圈成功！');history.go(-2); </script>";
                    die;
                }catch (\Exception $e){
                    throw new \Exception('缺少参数');
                }
            });

            $form->text('id', 'ID');
            if(request('city_id')){
                $city = City::find(request('city_id'));
                $form->radio('level','市区县')->options(['0' => '省', '1'=> '市', '2'=>'区'])->default($city->level);
                $form->text('zonename', '区县')->value($city->zonename);
                $form->text('code', '编号')->value($city->code);
                $form->text('parentid', '父级id')->value($city->parentid);
                $form->text('province', '省份')->value($city->province);
                $form->text('city', '城市')->value($city->city);
                $form->text('street_address', '街道')->rules('requeird');
            }else {
                $form->text('zonename', '市区县');
                $form->text('province', '省份');
                $form->text('city', '城市');
                $form->text('street_address', '街道');
            }
        });
    }
}
